import os
from building import *

cwd = GetCurrentDir()

# 1. 最小系统必需文件（始终包含）
hal_driver_path = os.path.join(cwd, 'CubeMX_Config', 'Drivers', 'STM32G0xx_HAL_Driver', 'Src')
minimal_src = [
    'stm32g0xx_hal.c',              # HAL核心文件
    'stm32g0xx_hal_rcc.c',          # 时钟配置（必需）
    'stm32g0xx_hal_rcc_ex.c',       # 时钟扩展
    'stm32g0xx_hal_cortex.c',       # 内核相关
	'stm32g0xx_hal_gpio.c',			#
	'stm32g0xx_hal_dma.c',			#
	'stm32g0xx_hal_exti.c',			#
	'stm32g0xx_hal_flash.c',		#
	'stm32g0xx_hal_flash_ex.c',		#
	'stm32g0xx_hal_pwr.c',			#
	'stm32g0xx_hal_pwr_ex.c',		#
]

# 2. 可选外设文件（根据Kconfig配置添加）
optional_src = []
# ADC外设
if GetDepend('USER_USING_ADC') or GetDepend('BSP_USING_ADC'):
    # 添加所有ADC相关的HAL和LL驱动文件
    optional_src.append('stm32g0xx_hal_adc.c')
    optional_src.append('stm32g0xx_hal_adc_ex.c')
    optional_src.append('stm32g0xx_ll_adc.c')
    
# SPI外设
if GetDepend('USER_USING_SPI') or GetDepend('BSP_USING_SPI'):
    optional_src.append('stm32g0xx_hal_spi.c')
    optional_src.append('stm32g0xx_hal_spi_ex.c')
    optional_src.append('stm32g0xx_ll_spi.c')
    
# UART外设
if GetDepend('USER_USING_UART') or GetDepend('BSP_USING_UART'):
    optional_src.append('stm32g0xx_hal_uart.c')
    optional_src.append('stm32g0xx_hal_uart_ex.c')
    optional_src.append('stm32g0xx_hal_usart.c')  # USART驱动
    optional_src.append('stm32g0xx_hal_usart_ex.c')
    optional_src.append('stm32g0xx_ll_usart.c')


# I2C外设
if GetDepend('USER_USING_I2C') or GetDepend('BSP_USING_I2C'):
    optional_src.append('stm32g0xx_hal_i2c.c')
    optional_src.append('stm32g0xx_hal_i2c_ex.c')
    optional_src.append('stm32g0xx_ll_i2c.c')

# DMA外设
if GetDepend('USER_USING_DMA') or GetDepend('BSP_USING_DMA'):
    optional_src.append('stm32g0xx_hal_dma.c')
    optional_src.append('stm32g0xx_hal_dma_ex.c')
    optional_src.append('stm32g0xx_ll_dma.c')

# I2S外设
if GetDepend('USER_USING_I2S') or GetDepend('BSP_USING_I2S'):
    optional_src.append('stm32g0xx_hal_i2s.c')

    
# RTC外设
if GetDepend('USER_USING_RTC') or GetDepend('BSP_USING_RTC'):
    optional_src.append('stm32g0xx_hal_rtc.c')
    optional_src.append('stm32g0xx_hal_rtc_ex.c')
    optional_src.append('stm32g0xx_ll_rtc.c')
    
# PWR外设
if GetDepend('USER_USING_PWR') or GetDepend('BSP_USING_PWR'):
    optional_src.append('stm32g0xx_hal_pwr.c')
    optional_src.append('stm32g0xx_hal_pwr_ex.c')
    optional_src.append('stm32g0xx_ll_pwr.c')
    
# TIMER外设
if GetDepend('USER_USING_TIMER') or GetDepend('BSP_USING_TIMER'):
    optional_src.append('stm32g0xx_hal_tim.c')
    optional_src.append('stm32g0xx_hal_tim_ex.c')
    optional_src.append('stm32g0xx_ll_tim.c')
    
# CRC外设
if GetDepend('USER_USING_CRC') or GetDepend('BSP_USING_CRC'):
    optional_src.append('stm32g0xx_hal_crc.c')
    optional_src.append('stm32g0xx_hal_crc_ex.c')
    optional_src.append('stm32g0xx_ll_crc.c')
    
if GetDepend('USER_USING_EXTI') or GetDepend('BSP_USING_EXTI'):
    optional_src.append('stm32g0xx_hal_exti.c')
    optional_src.append('stm32g0xx_ll_exti.c')
    
# USB外设
if GetDepend('USER_USING_USB') or GetDepend('BSP_USING_USB'):
    optional_src.append('stm32g0xx_hal_pcd.c')
    optional_src.append('stm32g0xx_hal_pcd_ex.c')
    optional_src.append('stm32g0xx_hal_hcd.c')
    optional_src.append('stm32g0xx_ll_usb.c')
    
# DAC外设
if GetDepend('USER_USING_DAC') or GetDepend('BSP_USING_DAC'):
    optional_src.append('stm32g0xx_hal_dac.c')
    optional_src.append('stm32g0xx_hal_dac_ex.c')
    optional_src.append('stm32g0xx_ll_dac.c')
    
# LPTIM低功耗定时器
if GetDepend('USER_USING_LPTIM') or GetDepend('BSP_USING_LPTIM'):
    optional_src.append('stm32g0xx_hal_lptim.c')
    optional_src.append('stm32g0xx_ll_lptim.c')
    
# RNG随机数生成器
if GetDepend('USER_USING_RNG') or GetDepend('BSP_USING_RNG'):
    optional_src.append('stm32g0xx_hal_rng.c')
    optional_src.append('stm32g0xx_ll_rng.c')
    
# SMBUS总线
if GetDepend('USER_USING_SMBUS') or GetDepend('BSP_USING_SMBUS'):
    optional_src.append('stm32g0xx_hal_smbus.c')
    optional_src.append('stm32g0xx_hal_smbus_ex.c')
    
# Flash操作
if GetDepend('USER_USING_FLASH') or GetDepend('BSP_USING_FLASH'):
    optional_src.append('stm32g0xx_hal_flash.c')
    optional_src.append('stm32g0xx_hal_flash_ex.c')
    
# FDCAN总线
if GetDepend('USER_USING_FDCAN') or GetDepend('BSP_USING_FDCAN'):
    optional_src.append('stm32g0xx_hal_fdcan.c')
    
# CEC消费电子控制
if GetDepend('USER_USING_CEC') or GetDepend('BSP_USING_CEC'):
    optional_src.append('stm32g0xx_hal_cec.c')
    
if GetDepend('USER_USING_IRDA') or GetDepend('BSP_USING_IRDA'):
    optional_src.append('stm32g0xx_hal_irda.c')
    
# SmartCard智能卡
if GetDepend('USER_USING_SMARTCARD'):
    optional_src.append('stm32g0xx_hal_smartcard.c')
    optional_src.append('stm32g0xx_hal_smartcard_ex.c')

# 3. 合并所有HAL驱动文件
hal_src = [os.path.join(hal_driver_path, f) for f in minimal_src + optional_src]

# 4. 板级支持包文件
board_src = Split('''
board.c
CubeMX_Config/Core/Src/stm32g0xx_hal_msp.c
CubeMX_Config/Core/Src/system_stm32g0xx.c
''')
board_src += [cwd + '/CubeMX_Config/stm32g070cbt6/MDK-ARM/startup_stm32g070xx.s']

# 5. 头文件路径配置
path =  [
    cwd,
    cwd + '/CubeMX_Config/Core/Inc',
    cwd + '/CubeMX_Config/Drivers/STM32G0xx_HAL_Driver/Inc',
    cwd + '/CubeMX_Config/CMSIS/Device/ST/STM32G0xx/Include',
    cwd + '/CubeMX_Config/Drivers/CMSIS/Device/ST/STM32G0xx/Include',
    cwd + '/CubeMX_Config/Drivers/CMSIS/Core/Include'
]

# 6. 分组定义
drivers_group = DefineGroup('Drivers', board_src, depend=[''], CPPPATH=path)
libraries_group = drivers_group + DefineGroup('Libraries', hal_src, depend=[''], CPPPATH=path)

# 7. 返回分组结构
Return('libraries_group')